package cmbchina.util.common;

import org.flowable.app.cusTaskExe.CusCheckItilTask;
import org.flowable.engine.TaskService;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by sevenlm on 2017/12/22.
 */
public class CusTaskComonOptUtils {

    private static final Logger logger = LoggerFactory.getLogger(CusCheckItilTask.class);

    /**
     * 处理流程信息
     * @param startFormMap
     * @param currentFormInfo
     * @param listener
     * @return
     */
    public Map<String,Object> optFormInfo(Map<String,Object> startFormMap, Map<String,Object> currentFormInfo, DelegateTask listener){

        Object value="";
        Map<String,Object> returnMap = new LinkedHashMap<>();
        Map<String,Object> currentValuesMap = (Map<String, Object>) currentFormInfo.get("values");
        Map<String,Object> startValuesMap = (Map<String, Object>) startFormMap.get("values");
        for(String key:currentValuesMap.keySet()){

            //先从流程全局变量中获取变量
            Object obj = listener.getVariable(key);
            if(currentValuesMap.get(key)!=null && currentValuesMap.get(key).toString().indexOf("请选择")!=0){
                value = currentValuesMap.get(key);
            }else if(obj == null){

                //如果获取不到再从开始节点获取
                value =  startValuesMap.get(key);
                //如果还是获取不到，拿当前节点本来的值
                if(value==null){
                    //
                    if(currentValuesMap.get(key)==null){

                    }
                    else{
                        //当前节点存在未配置参数的表单字段，预留后续处理
                    }
                    value = currentValuesMap.get(key);
                }
            }else{
                value = obj;
            }
            returnMap.put(key,value);


        }
        currentFormInfo.put("values",returnMap);
        return currentFormInfo;
    }



    public Map<String,Object> optFormInfoByService(Map<String,Object> startFormMap, Map<String,Object> currentFormInfo, TaskService taskService,String Taskid,String processInstId){

        Object value="";
        Map<String,Object> returnMap = new LinkedHashMap<>();
        Map<String,Object> currentValuesMap = (Map<String, Object>) currentFormInfo.get("values");
        Map<String,Object> startValuesMap = (Map<String, Object>) startFormMap.get("values");
        Map<String,Object> variableMap = new LinkedHashMap<>();
        variableMap = taskService.createTaskQuery().processInstanceId(processInstId).singleResult().getProcessVariables();
        for(String key:currentValuesMap.keySet()){

            //先从流程全局变量中获取变量
            Object obj = variableMap.get(key);
            //如果获取不到
            if(obj==null){

                //如果获取不到再从开始节点获取
                value =  startValuesMap.get(key);
                //如果还是获取不到，拿当前节点本来的值
                if(value==null){
                    //
                    if(currentValuesMap.get(key)==null){

                    }
                    else{
                        //当前节点存在未配置参数的表单字段，预留后续处理
                    }
                    value = currentValuesMap.get(key);
                }
            }else{
                value = obj;
            }
            returnMap.put(key,value);


        }
        currentFormInfo.put("values",returnMap);
        return currentFormInfo;
    }
}
